import logging

import reptile.plugins
from utils import LogManager
from utils import singleton


@singleton
class ReptilePool:
	def __init__(self, interval = 3600):
		self.work_pool = {}
		self.log = LogManager()

		# 加载爬虫插件
		self.log.log(self.__class__.__name__, "Plugin is loading")
		for class_name in reptile.plugins.__all__:
			cls = getattr(reptile.plugins, class_name)
			self.create_project(cls, interval)
		self.log.log(self.__class__.__name__, "Plugin loading is end")

	def create_project(self, work_project, interval):
		try:
			self.work_pool[work_project.__name__] = work_project(interval)
			self.log.log(self.__class__.__name__, "Loading Plugin {} success".format(work_project.__name__))
		except Exception as e:
			self.log.log(self.__class__.__name__,
						 "Loading Plugin {} failed : {}".format(work_project.__name__, e), logging.WARNING)

	def run(self):
		result = {}
		self.log.log(self.__class__.__name__, "Plugin is running!")
		for class_name, work_project in self.work_pool.items():
			if work_project.need_work():
				self.log.log(self.__class__.__name__, "Plugin {} is running".format(work_project.__class__.__name__))
				try:
					work_project.work()
					for data in work_project.get_work_data():
						if data['protocol'] not in result:
							result[data['protocol']] = []
						result[data['protocol']].append(data)
				except Exception as e:
					self.log.log(self.__class__.__name__, "Plugin {} running failed : {}".format(
						work_project.__class__.__name__,
						e
					), logging.WARNING)
				self.log.log(self.__class__.__name__,
							 "Plugin {} has finished running , obtaining {} pieces of data".format(
								 work_project.__class__.__name__,
								 len(work_project.get_work_data())))
		self.log.log(self.__class__.__name__, "Plugin ends running")
		return result
